/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.model.team.legion;

import com.aionemu.commons.database.dao.DAOManager;
import com.aionemu.gameserver.configs.main.LegionConfig;
import com.aionemu.gameserver.dao.PlayerDAO;
import com.aionemu.gameserver.model.Race;
import com.aionemu.gameserver.model.gameobjects.Npc;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.PlayerCommonData;
import com.aionemu.gameserver.model.team.interfaces.ITeamMember;
import com.aionemu.gameserver.model.team.legion.enums.eLegionRight;
import com.aionemu.gameserver.network.aion.serverpackets.SM_LEGION_UPDATE_MEMBER;
import com.aionemu.gameserver.utils.PacketSendUtility;

/**
 * @author Simple, Lyahim
 * 
 */
public class LegionMember implements ITeamMember
{
	private static final int	LP_CENT_NONE	= 0x60;
	private int					objectId		= 0;
	private String				nickname		= "";
	private String				selfIntro		= "";
	private LegionRank			rank			= LegionRank.LEGIONARY;
	private final int			legionId;
	private PlayerCommonData    commonData;

	public LegionMember(int id, int legionId, LegionRank rank, String nick, String intro)
	{
		this.objectId = id;
		this.legionId = legionId;
		this.rank = rank;
		this.nickname = nick;
		this.selfIntro = intro;
		this.commonData = DAOManager.getDAO(PlayerDAO.class).loadPlayerCommonData(id);
	}

	/**
	 * @return the legion
	 */
	public Legion getLegion()
	{
		return Legion.getLegionById(legionId);
	}

	
	/**
	 * @return Returns the legionId.
	 */
	public int getLegionId()
	{
		return legionId;
	}

	/**
	 * @param rank
	 *            the rank to set
	 */
	public void setRank(LegionRank rank)
	{
		this.rank = rank;
	}

	/**
	 * @return the rank
	 */
	public LegionRank getRank()
	{
		return rank;
	}

	public boolean isBrigadeGeneral()
	{
		return rank == LegionRank.BRIGADE_GENERAL;
	}

	/**
	 * @param nickname
	 *            the nickname to set
	 * @return true if nick valid, else false
	 */
	public boolean setNickname(String nickname)
	{
		if (isValidNickname(selfIntro))
		{
			this.nickname = nickname;
			return true;
		}
		return false;

	}

	/**
	 * @return the nickname
	 */
	public String getNickname()
	{
		return nickname;
	}

	/**
	 * @param selfIntro
	 *            the selfIntro to set
	 * @return true if intro is valid, else false
	 */
	public boolean setSelfIntro(String selfIntro)
	{
		if (isValidSelfIntro(selfIntro))
		{
			this.selfIntro = selfIntro;
			return true;
		}
		return false;
	}

	/**
	 * @return the selfIntro
	 */
	public String getSelfIntro()
	{
		return selfIntro;
	}

	/**
	 * @return the objectId
	 */
	@Override
	public int getObjectId()
	{
		return objectId;
	}

	public boolean hasRights(eLegionRight type)
	{
		if (getRank() == LegionRank.BRIGADE_GENERAL)
			return true;

		int legionarPermission2 = getLegion().getLegionarPermission2();
		int centurionPermission1 = getLegion().getCenturionPermission1() - LP_CENT_NONE;
		int centurionPermission2 = getLegion().getCenturionPermission2();

		switch (type)
		{
			case INVITE:
				if (getRank().canInviteToLegion(centurionPermission1))
					return true;

			case KICK:
				if (getRank().canKickFromLegion(centurionPermission1))
					return true;

			case WAREHOUSE:
				if (getRank().canUseLegionWarehouse(centurionPermission1))
					return true;

			case ANNOUNCEMENT:
				if (getRank().canEditAnnouncement(centurionPermission2))
					return true;

			case ARTIFACT:
				if (getRank().canUseArtifact(centurionPermission2))
					return true;

			case GATEGUARDIAN:
				if (getRank().canUseGateGuardianStone(centurionPermission2, legionarPermission2))
					return true;
		}
		return false;

	}

	public PlayerCommonData getCommondata()
	{
		return commonData;
	}

	private boolean isValidSelfIntro(String name)
	{
		return LegionConfig.SELF_INTRO_PATTERN.matcher(name).matches();
	}

	private boolean isValidNickname(String name)
	{
		return LegionConfig.NICKNAME_PATTERN.matcher(name).matches();
	}

	public Player getPlayer()
	{
		return commonData.getPlayer();
	}

	public void updateInfo(Legion legion, int messageId, String text)
	{
		PacketSendUtility.broadcastPacketToTeam(getPlayer(), legion, new SM_LEGION_UPDATE_MEMBER(this, messageId, text));
	}

	public void updateInfo(Legion legion)
	{
		PacketSendUtility.broadcastPacketToTeam(getPlayer(), legion, new SM_LEGION_UPDATE_MEMBER(this, 0, ""));
	}

	public boolean isOnline()
	{
		return commonData.isOnline();
	}

	@Override
	public void getReward(Npc owner)
	{
	}

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.model.team.interfaces.ITeamMember#getRace()
	 */
	@Override
	public Race getRace()
	{
		return this.getCommondata().getRace();
	}
}
